<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="redis,redis集群," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.0" />






<meta name="description" content="我们已经搭建了cluster模式集群，那么redis的cluster集群是如何做到高可用容错以及扩容的呢。 master节点宕机如果cluster集群中的master节点宕机，cluster是如何做故障转移。我们将9001上的redis服务关闭redis-cli  -c -p 9001127.0.0.1:9001&amp;gt; shutdown not connected&amp;gt; 然后登录到9002客户">
<meta name="keywords" content="redis,redis集群">
<meta property="og:type" content="article">
<meta property="og:title" content="redis集群模式的容错扩容">
<meta property="og:url" content="https://gitee.com/shuhao/2017/12/21/redis集群模式的容错扩容/index.html">
<meta property="og:site_name" content="Niki">
<meta property="og:description" content="我们已经搭建了cluster模式集群，那么redis的cluster集群是如何做到高可用容错以及扩容的呢。 master节点宕机如果cluster集群中的master节点宕机，cluster是如何做故障转移。我们将9001上的redis服务关闭redis-cli  -c -p 9001127.0.0.1:9001&amp;gt; shutdown not connected&amp;gt; 然后登录到9002客户">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-12-23T01:59:53.690Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="redis集群模式的容错扩容">
<meta name="twitter:description" content="我们已经搭建了cluster模式集群，那么redis的cluster集群是如何做到高可用容错以及扩容的呢。 master节点宕机如果cluster集群中的master节点宕机，cluster是如何做故障转移。我们将9001上的redis服务关闭redis-cli  -c -p 9001127.0.0.1:9001&amp;gt; shutdown not connected&amp;gt; 然后登录到9002客户">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/shuhao/2017/12/21/redis集群模式的容错扩容/"/>





  <title> redis集群模式的容错扩容 | Niki </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cb9f0585e406f878cb2f29c89ad3cdb7";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Niki</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">天道酬勤</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-categories"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/shuhao/blog/2017/12/21/redis集群模式的容错扩容/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fang ShuHao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/head.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Niki">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                redis集群模式的容错扩容
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-21T15:28:12+08:00">
                2017-12-21
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/redis/" itemprop="url" rel="index">
                    <span itemprop="name">redis</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>我们已经<a href="http://shuhao.gitee.io/blog/2017/12/18/redis%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F%E6%90%AD%E5%BB%BA/" target="_blank" rel="external">搭建了cluster模式集群</a>，那么redis的cluster集群是如何做到高可用容错以及扩容的呢。</p>
<h3 id="master节点宕机"><a href="#master节点宕机" class="headerlink" title="master节点宕机"></a>master节点宕机</h3><p>如果cluster集群中的master节点宕机，cluster是如何做故障转移。我们将9001上的redis服务关闭<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">redis-cli  -c -p 9001</span><br><span class="line">127.0.0.1:9001&gt; shutdown </span><br><span class="line">not connected&gt;</span><br></pre></td></tr></table></figure></p>
<p>然后登录到9002客户端查看cluster的信息<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">redis-cli -c -p 9002</span><br><span class="line">127.0.0.1:9002&gt; cluster info</span><br><span class="line">cluster_state:ok</span><br><span class="line">cluster_slots_assigned:16384</span><br><span class="line">cluster_slots_ok:16384</span><br><span class="line">cluster_slots_pfail:0</span><br><span class="line">cluster_slots_fail:0</span><br><span class="line">cluster_known_nodes:6</span><br><span class="line">cluster_size:3</span><br><span class="line">cluster_current_epoch:7</span><br><span class="line">cluster_my_epoch:2</span><br><span class="line">cluster_stats_messages_ping_sent:73742</span><br><span class="line">cluster_stats_messages_pong_sent:72649</span><br><span class="line">cluster_stats_messages_meet_sent:2</span><br><span class="line">cluster_stats_messages_fail_sent:4</span><br><span class="line">cluster_stats_messages_auth-ack_sent:1</span><br><span class="line">cluster_stats_messages_sent:146398</span><br><span class="line">cluster_stats_messages_ping_received:72646</span><br><span class="line">cluster_stats_messages_pong_received:69887</span><br><span class="line">cluster_stats_messages_meet_received:3</span><br><span class="line">cluster_stats_messages_fail_received:1</span><br><span class="line">cluster_stats_messages_auth-req_received:1</span><br><span class="line">cluster_stats_messages_received:142538</span><br></pre></td></tr></table></figure></p>
<p>可以看到集群的状态显示为可用，slot也全部可用。但是_cluster_stats_messages_fail_received:1_显示cluster失败了一条状态信息。</p>
<p>再查看cluster中节点的状态<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">cluster nodes</span><br><span class="line">a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003@19003 master - 0 1513841641000 3 connected 10923-16383</span><br><span class="line">2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002@19002 myself,master - 0 1513841642000 2 connected 5461-10922</span><br><span class="line">c3557afd64b0e913f9cac834c167565a3809b8fd 127.0.0.1:9004@19004 master - 0 1513841641805 7 connected 0-5460</span><br><span class="line">55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005@19005 slave 2a4c6b918e1ab5975541b0a1569c384833826ae7 0 1513841642815 5 connected</span><br><span class="line">923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006@19006 slave a0acca7be369c07be9a1cf7e20e73893ad07dae0 0 1513841640000 6 connected</span><br><span class="line">1a316a85cd0330bd06cc68af55a9029c2826ea06 127.0.0.1:9001@19001 master,fail - 1513841127705 1513841123000 1 disconnected</span><br></pre></td></tr></table></figure></p>
<p>可以看到我们原来9001上的master节点为fail。并且9004为选举顶替9001成为新的master节点 9001上的slot也分配到了9004上</p>
<h3 id="宕机的9001恢复"><a href="#宕机的9001恢复" class="headerlink" title="宕机的9001恢复"></a>宕机的9001恢复</h3><p>我们重新启动9001<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">sudo redis-server /usr/local/redis/redis-cluster/9001/redis.conf </span><br><span class="line">sudo redis-cli -c -p 9001</span><br><span class="line">127.0.0.1:9001&gt; cluster nodes</span><br><span class="line">1a316a85cd0330bd06cc68af55a9029c2826ea06 127.0.0.1:9001@19001 myself,slave c3557afd64b0e913f9cac834c167565a3809b8fd 0 1513841776000 1 connected</span><br><span class="line">a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003@19003 master - 0 1513841774179 3 connected 10923-16383</span><br><span class="line">c3557afd64b0e913f9cac834c167565a3809b8fd 127.0.0.1:9004@19004 master - 0 1513841778199 7 connected 0-5460</span><br><span class="line">923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006@19006 slave a0acca7be369c07be9a1cf7e20e73893ad07dae0 0 1513841776000 6 connected</span><br><span class="line">2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002@19002 master - 0 1513841776189 2 connected 5461-10922</span><br><span class="line">55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005@19005 slave 2a4c6b918e1ab5975541b0a1569c384833826ae7 0 1513841777194 5 connected</span><br><span class="line"></span><br><span class="line">cluster info</span><br><span class="line">cluster_state:ok</span><br><span class="line">cluster_slots_assigned:16384</span><br><span class="line">cluster_slots_ok:16384</span><br><span class="line">cluster_slots_pfail:0</span><br><span class="line">cluster_slots_fail:0</span><br><span class="line">cluster_known_nodes:6</span><br><span class="line">cluster_size:3</span><br><span class="line">cluster_current_epoch:7</span><br><span class="line">cluster_my_epoch:7</span><br><span class="line">cluster_stats_messages_ping_sent:142</span><br><span class="line">cluster_stats_messages_pong_sent:144</span><br><span class="line">cluster_stats_messages_sent:286</span><br><span class="line">cluster_stats_messages_ping_received:144</span><br><span class="line">cluster_stats_messages_pong_received:142</span><br><span class="line">cluster_stats_messages_received:286</span><br></pre></td></tr></table></figure></p>
<p>我们看到，恢复的9001成为了c3557afd64b0e913f9cac834c167565a3809b8fd（9004）的salve节点，重新加如到cluster集群中。</p>
<ul>
<li>至于slave节点宕机，我们就不看了。</li>
</ul>
<h3 id="cluster移除节点"><a href="#cluster移除节点" class="headerlink" title="cluster移除节点"></a>cluster移除节点</h3><p>在9001客户端移除9001节点<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">127.0.0.1:9001&gt; cluster forget 1a316a85cd0330bd06cc68af55a9029c2826ea06</span><br><span class="line">(error) ERR I tried hard but I can&apos;t forget myself...</span><br></pre></td></tr></table></figure></p>
<p>可以看到无法从本节点客户端移除cluster中的本节点<br>换到9002客户端再试一遍<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CLUSTER FORGET 1a316a85cd0330bd06cc68af55a9029c2826ea06</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:9002&gt; CLUSTER NODES</span><br><span class="line">a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003@19003 master - 0 1513845369102 3 connected 10923-16383</span><br><span class="line">2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002@19002 myself,master - 0 1513845368000 2 connected 5461-10922</span><br><span class="line">c3557afd64b0e913f9cac834c167565a3809b8fd 127.0.0.1:9004@19004 master - 0 1513845370000 7 connected 0-5460</span><br><span class="line">55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005@19005 slave 2a4c6b918e1ab5975541b0a1569c384833826ae7 0 1513845371115 5 connected</span><br><span class="line">923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006@19006 slave a0acca7be369c07be9a1cf7e20e73893ad07dae0 0 1513845370111 6 connected</span><br></pre></td></tr></table></figure></p>
<p>我们可以看到在9002上成功将9001从cluster集群中移除。9001虽然已从cluster中移除，但是并没有关闭。仍然是存活状态。</p>
<h3 id="cluster移除、添加cluster内的节点"><a href="#cluster移除、添加cluster内的节点" class="headerlink" title="cluster移除、添加cluster内的节点"></a>cluster移除、添加cluster内的节点</h3><p>我们将刚才删除的9001节点再次添加到cluster中<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">127.0.0.1:9002&gt; CLUSTER MEET 127.0.0.1 9001</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:9002&gt; cluster nodes</span><br><span class="line">a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003@19003 master - 0 1513845539000 3 connected 10923-16383</span><br><span class="line">2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002@19002 myself,master - 0 1513845538000 2 connected 5461-10922</span><br><span class="line">c3557afd64b0e913f9cac834c167565a3809b8fd 127.0.0.1:9004@19004 master - 0 1513845537000 7 connected 0-5460</span><br><span class="line">55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005@19005 slave 2a4c6b918e1ab5975541b0a1569c384833826ae7 0 1513845539971 5 connected</span><br><span class="line">923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006@19006 slave a0acca7be369c07be9a1cf7e20e73893ad07dae0 0 1513845537962 6 connected</span><br><span class="line">1a316a85cd0330bd06cc68af55a9029c2826ea06 127.0.0.1:9001@19001 slave c3557afd64b0e913f9cac834c167565a3809b8fd 0 1513845538000 7 connected</span><br></pre></td></tr></table></figure></p>
<p>9001节点又添加到cluster中并成为c3557afd64b0e913f9cac834c167565a3809b8fd（9004）的从节点。</p>
<p>如果我们将9001的master节点9004删除会是什么情形，来看下<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">127.0.0.1:9002&gt; CLUSTER FORGET c3557afd64b0e913f9cac834c167565a3809b8fd</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:9002&gt; CLUSTER NODES</span><br><span class="line">a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003@19003 master - 0 1513845659000 3 connected 10923-16383</span><br><span class="line">2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002@19002 myself,master - 0 1513845660000 2 connected 5461-10922</span><br><span class="line">55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005@19005 slave 2a4c6b918e1ab5975541b0a1569c384833826ae7 0 1513845661610 5 connected</span><br><span class="line">923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006@19006 slave a0acca7be369c07be9a1cf7e20e73893ad07dae0 0 1513845660604 6 connected</span><br><span class="line">1a316a85cd0330bd06cc68af55a9029c2826ea06 127.0.0.1:9001@19001 slave - 0 1513845659601 7 connected</span><br><span class="line"></span><br><span class="line">127.0.0.1:9002&gt; CLUSTER INFO</span><br><span class="line">cluster_state:fail</span><br><span class="line">cluster_slots_assigned:10923</span><br><span class="line">cluster_slots_ok:10923</span><br><span class="line">cluster_slots_pfail:0</span><br><span class="line">cluster_slots_fail:0</span><br><span class="line">cluster_known_nodes:5</span><br><span class="line">cluster_size:2</span><br><span class="line">cluster_current_epoch:7</span><br><span class="line">cluster_my_epoch:2</span><br><span class="line">cluster_stats_messages_ping_sent:80544</span><br><span class="line">cluster_stats_messages_pong_sent:77052</span><br><span class="line">cluster_stats_messages_meet_sent:6</span><br><span class="line">cluster_stats_messages_fail_sent:4</span><br><span class="line">cluster_stats_messages_auth-ack_sent:1</span><br><span class="line">cluster_stats_messages_sent:157607</span><br><span class="line">cluster_stats_messages_ping_received:77049</span><br><span class="line">cluster_stats_messages_pong_received:74191</span><br><span class="line">cluster_stats_messages_meet_received:3</span><br><span class="line">cluster_stats_messages_fail_received:1</span><br><span class="line">cluster_stats_messages_auth-req_received:1</span><br><span class="line">cluster_stats_messages_received:151245</span><br></pre></td></tr></table></figure></p>
<h4 id="9004节点删除成功，并且cluster的状态为失败！所以如果移除cluster的slave节点，则不影响cluster，如果移除master节点，cluster的状态为fail"><a href="#9004节点删除成功，并且cluster的状态为失败！所以如果移除cluster的slave节点，则不影响cluster，如果移除master节点，cluster的状态为fail" class="headerlink" title="9004节点删除成功，并且cluster的状态为失败！所以如果移除cluster的slave节点，则不影响cluster，如果移除master节点，cluster的状态为fail"></a><em>9004节点删除成功，并且cluster的状态为失败！所以如果移除cluster的slave节点，则不影响cluster，如果移除master节点，cluster的状态为fail</em></h4><h3 id="cluster-扩展节点"><a href="#cluster-扩展节点" class="headerlink" title="cluster 扩展节点"></a>cluster 扩展节点</h3><p>复制两个节点（通过复制9001的redis.conf）9007,9008<br>将9008加入到cluster<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">/redis-4.0.2/src/redis-trib.rb  add-node 127.0.0.1:9008 127.0.0.1:9001</span><br></pre></td></tr></table></figure></p>
<p>因为add-node 规定第一个参数为新节点的ip:port 第二个参数为cluster中任一节点的ip:port(去中心化)。<br>添加成功，可以看到9008被加入到cluster变成了master节点<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">127.0.0.1:9001&gt; CLUSTER NODES</span><br><span class="line">1a316a85cd0330bd06cc68af55a9029c2826ea06 127.0.0.1:9001@19001 myself,slave c3557afd64b0e913f9cac834c167565a3809b8fd 0 1513847004000 1 connected</span><br><span class="line">a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003@19003 master - 0 1513847002000 3 connected 10923-16383</span><br><span class="line">c3557afd64b0e913f9cac834c167565a3809b8fd 127.0.0.1:9004@19004 master - 0 1513847004353 7 connected 0-5460</span><br><span class="line">923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006@19006 slave a0acca7be369c07be9a1cf7e20e73893ad07dae0 0 1513847003000 6 connected</span><br><span class="line">245c19be72937eb076d45101717947fd31ed7693 127.0.0.1:9008@19008 master - 0 1513847003351 0 connected</span><br><span class="line">2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002@19002 master - 0 1513847003000 2 connected 5461-10922</span><br><span class="line">55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005@19005 slave 2a4c6b918e1ab5975541b0a1569c384833826ae7 0 1513847001345 5 connected</span><br></pre></td></tr></table></figure></p>
<p>同上再将9007加入到cluster，因为我们在创建集群时，配置了主从比为1，那么我们新添加进去的9007会被设置为slave节点平衡cluster中的主从比么？<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">127.0.0.1:9001&gt; CLUSTER NODES</span><br><span class="line">1a316a85cd0330bd06cc68af55a9029c2826ea06 127.0.0.1:9001@19001 myself,slave c3557afd64b0e913f9cac834c167565a3809b8fd 0 1513847067000 1 connected</span><br><span class="line">a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003@19003 master - 0 1513847068000 3 connected 10923-16383</span><br><span class="line">c3557afd64b0e913f9cac834c167565a3809b8fd 127.0.0.1:9004@19004 master - 0 1513847067000 7 connected 0-5460</span><br><span class="line">923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006@19006 slave a0acca7be369c07be9a1cf7e20e73893ad07dae0 0 1513847068000 6 connected</span><br><span class="line">245c19be72937eb076d45101717947fd31ed7693 127.0.0.1:9008@19008 master - 0 1513847069675 0 connected</span><br><span class="line">2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002@19002 master - 0 1513847069000 2 connected 5461-10922</span><br><span class="line">55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005@19005 slave 2a4c6b918e1ab5975541b0a1569c384833826ae7 0 1513847068670 5 connected</span><br><span class="line">1a60a7e605ac1fe0f0d9bf9817d7b261bac76070 127.0.0.1:9007@19007 master - 0 1513847070680 8 connected</span><br></pre></td></tr></table></figure></p>
<p>从结果上来看，并没有将9007设置为slave，相反是设置成了master。看来add-node 添加节点默认是配置为master节点。那么怎么添加slave节点呢？可以通过以下配置<br>add-node –slave –master-id <argument> ip:port cluster_ip:cluster_port<br>argument为master的node_id, ip:port为新节点的ip和port， cluster_ip 和cluster_port为cluster中任一节点的ip和port<br>我们把9007删掉，再通过上面的方法加入到cluster<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">[ERR] Node 127.0.0.1:9007 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.</span><br><span class="line">127.0.0.1:9001&gt; CLUSTER NODES</span><br><span class="line">1a316a85cd0330bd06cc68af55a9029c2826ea06 127.0.0.1:9001@19001 myself,slave c3557afd64b0e913f9cac834c167565a3809b8fd 0 1513847787000 1 connected</span><br><span class="line">a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003@19003 master - 0 1513847786000 3 connected 10923-16383</span><br><span class="line">c3557afd64b0e913f9cac834c167565a3809b8fd 127.0.0.1:9004@19004 master - 0 1513847786000 7 connected 0-5460</span><br><span class="line">923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006@19006 slave a0acca7be369c07be9a1cf7e20e73893ad07dae0 0 1513847784000 6 connected</span><br><span class="line">245c19be72937eb076d45101717947fd31ed7693 127.0.0.1:9008@19008 master - 0 1513847786290 0 connected</span><br><span class="line">2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002@19002 master - 0 1513847787295 2 connected 5461-10922</span><br><span class="line">55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005@19005 slave 2a4c6b918e1ab5975541b0a1569c384833826ae7 0 1513847788301 5 connected</span><br><span class="line">1a60a7e605ac1fe0f0d9bf9817d7b261bac76070 127.0.0.1:9007@19007 master - 0 1513847785283 8 connected</span><br></pre></td></tr></table></figure></argument></p>
<p>我们发现，这样不可以，因为9007已经添加过，无法执行这个操作，（9007重新添加到cluster中）我们再复制一个节点9009。再添加一次<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">/redis-4.0.2/src/redis-trib.rb add-node --slave --master-id 245c19be72937eb076d45101717947fd31ed7693 127.0.0.1:9009 127.0.0.1:9001</span><br><span class="line">127.0.0.1:9001&gt; CLUSTER NODES</span><br><span class="line">a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003@19003 master - 0 1513848030000 3 connected 10923-16383</span><br><span class="line">1a316a85cd0330bd06cc68af55a9029c2826ea06 127.0.0.1:9001@19001 myself,slave c3557afd64b0e913f9cac834c167565a3809b8fd 0 1513848026000 1 connected</span><br><span class="line">923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006@19006 slave a0acca7be369c07be9a1cf7e20e73893ad07dae0 0 1513848028449 6 connected</span><br><span class="line">c3557afd64b0e913f9cac834c167565a3809b8fd 127.0.0.1:9004@19004 master - 0 1513848029000 7 connected 0-5460</span><br><span class="line">2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002@19002 master - 0 1513848028000 2 connected 5461-10922</span><br><span class="line">245c19be72937eb076d45101717947fd31ed7693 127.0.0.1:9008@19008 master - 0 1513848030457 0 connected</span><br><span class="line">12dc34cddbaa43e3171072bf7bfcd8b7f5787222 127.0.0.1:9009@19009 slave 245c19be72937eb076d45101717947fd31ed7693 0 1513848027444 0 connected</span><br><span class="line">55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005@19005 slave 2a4c6b918e1ab5975541b0a1569c384833826ae7 0 1513848028000 5 connected</span><br><span class="line">1a60a7e605ac1fe0f0d9bf9817d7b261bac76070 127.0.0.1:9007@19007 master - 0 1513848031461 8 connected</span><br></pre></td></tr></table></figure></p>
<p>可以看到9009已经加入到cluster中，并且成为245c19be72937eb076d45101717947fd31ed7693（9008）的slave节点。</p>
<h3 id="slot槽分配"><a href="#slot槽分配" class="headerlink" title="slot槽分配"></a>slot槽分配</h3><p>我们添加了新的节点到cluster中，虽然配置为了master节点，但是，并没有分配slot，所以并不能存储数据，所以我们还需要重新分配我们的slot槽给我们新添加进来的master节点。<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">/redis-4.0.2/src/redis-trib.rb  reshard 127.0.0.1:9001</span><br><span class="line">&gt;&gt;&gt; Performing Cluster Check (using node 127.0.0.1:9001)</span><br><span class="line">S: 1a316a85cd0330bd06cc68af55a9029c2826ea06 127.0.0.1:9001</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates c3557afd64b0e913f9cac834c167565a3809b8fd</span><br><span class="line">M: a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003</span><br><span class="line">   slots:10923-16383 (5461 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates a0acca7be369c07be9a1cf7e20e73893ad07dae0</span><br><span class="line">M: c3557afd64b0e913f9cac834c167565a3809b8fd 127.0.0.1:9004</span><br><span class="line">   slots:0-5460 (5461 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">M: 2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002</span><br><span class="line">   slots:5461-10922 (5462 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">M: 245c19be72937eb076d45101717947fd31ed7693 127.0.0.1:9008</span><br><span class="line">   slots: (0 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 12dc34cddbaa43e3171072bf7bfcd8b7f5787222 127.0.0.1:9009</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 245c19be72937eb076d45101717947fd31ed7693</span><br><span class="line">S: 55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 2a4c6b918e1ab5975541b0a1569c384833826ae7</span><br><span class="line">M: 1a60a7e605ac1fe0f0d9bf9817d7b261bac76070 127.0.0.1:9007</span><br><span class="line">   slots: (0 slots) master</span><br><span class="line">   0 additional replica(s)</span><br><span class="line">[OK] All nodes agree about slots configuration.</span><br><span class="line">&gt;&gt;&gt; Check for open slots...</span><br><span class="line">&gt;&gt;&gt; Check slots coverage...</span><br><span class="line">[OK] All 16384 slots covered.</span><br><span class="line">How many slots do you want to move (from 1 to 16384)?</span><br></pre></td></tr></table></figure></p>
<p>可以把分配的过程理解成打扑克牌，all表示大家重新洗牌；输入某个主节点的node id，然后在输入done的话，就好比从某个节点，抽牌。</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">/redis-4.0.2/src/redis-trib.rb  rebalance 127.0.0.1:9002</span><br></pre></td></tr></table></figure>
<p>这个命令会自动帮我们在cluster中重新分配我们的slot。</p>
<p>目前，我们队cluster进行了扩容，已经缩减节点的操作，并且重新分配了slot。</p>
<p>附注：<br>redis-trib help 可以查看redis-trib的命令。帮助我们更好的操作我们的cluster<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">redis-trib.rb </span><br><span class="line">Usage: redis-trib &lt;command&gt; &lt;options&gt; &lt;arguments ...&gt;</span><br><span class="line"></span><br><span class="line">  create          host1:port1 ... hostN:portN</span><br><span class="line">                  --replicas &lt;arg&gt;</span><br><span class="line">  check           host:port</span><br><span class="line">  info            host:port</span><br><span class="line">  fix             host:port</span><br><span class="line">                  --timeout &lt;arg&gt;</span><br><span class="line">  reshard         host:port</span><br><span class="line">                  --from &lt;arg&gt;</span><br><span class="line">                  --to &lt;arg&gt;</span><br><span class="line">                  --slots &lt;arg&gt;</span><br><span class="line">                  --yes</span><br><span class="line">                  --timeout &lt;arg&gt;</span><br><span class="line">                  --pipeline &lt;arg&gt;</span><br><span class="line">  rebalance       host:port</span><br><span class="line">                  --weight &lt;arg&gt;</span><br><span class="line">                  --auto-weights</span><br><span class="line">                  --use-empty-masters</span><br><span class="line">                  --timeout &lt;arg&gt;</span><br><span class="line">                  --simulate</span><br><span class="line">                  --pipeline &lt;arg&gt;</span><br><span class="line">                  --threshold &lt;arg&gt;</span><br><span class="line">  add-node        new_host:new_port existing_host:existing_port</span><br><span class="line">                  --slave</span><br><span class="line">                  --master-id &lt;arg&gt;</span><br><span class="line">  del-node        host:port node_id</span><br><span class="line">  set-timeout     host:port milliseconds</span><br><span class="line">  call            host:port command arg arg .. arg</span><br><span class="line">  import          host:port</span><br><span class="line">                  --from &lt;arg&gt;</span><br><span class="line">                  --copy</span><br><span class="line">                  --replace</span><br><span class="line">  help            (show this help)</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>参考</p>
</blockquote>
<p><a href="http://blog.51yip.com/nosql/1726.html" target="_blank" rel="external">redis cluster 添加 删除 重分配 节点</a></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/redis/" rel="tag"># redis</a>
          
            <a href="/blog/tags/redis集群/" rel="tag"># redis集群</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2017/12/19/redis主从模式搭建/" rel="next" title="redis主从模式搭建">
                <i class="fa fa-chevron-left"></i> redis主从模式搭建
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2017/12/23/JVM随记/" rel="prev" title="JVM随记">
                JVM随记 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNjY5NS8xMzIzMA=="></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/head.jpg"
               alt="Fang ShuHao" />
          <p class="site-author-name" itemprop="name">Fang ShuHao</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives">
                <span class="site-state-item-count">135</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories">
                <span class="site-state-item-count">35</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/blog/tags">
                <span class="site-state-item-count">93</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#master节点宕机"><span class="nav-number">1.</span> <span class="nav-text">master节点宕机</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#宕机的9001恢复"><span class="nav-number">2.</span> <span class="nav-text">宕机的9001恢复</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cluster移除节点"><span class="nav-number">3.</span> <span class="nav-text">cluster移除节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cluster移除、添加cluster内的节点"><span class="nav-number">4.</span> <span class="nav-text">cluster移除、添加cluster内的节点</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#9004节点删除成功，并且cluster的状态为失败！所以如果移除cluster的slave节点，则不影响cluster，如果移除master节点，cluster的状态为fail"><span class="nav-number">4.1.</span> <span class="nav-text">9004节点删除成功，并且cluster的状态为失败！所以如果移除cluster的slave节点，则不影响cluster，如果移除master节点，cluster的状态为fail</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cluster-扩展节点"><span class="nav-number">5.</span> <span class="nav-text">cluster 扩展节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#slot槽分配"><span class="nav-number">6.</span> <span class="nav-text">slot槽分配</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Fang ShuHao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  



  
  

  

  

  

  


  

</body>
</html>
